﻿// ***********************************************************************
// Assembly         : GUI
// Author           : Disco
// Created          : 05-20-2014
//
// Last Modified By : Kaj Disco + Kristian
// Last Modified On : 05-20-2014
// ***********************************************************************
// <copyright file="UIAddFriends.xaml.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using GUI.RLAServiceReference;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for UIAddFriends.xaml
    /// </summary>
    public partial class UIAddFriends : Window
    {
        private RealLifeServiceClient _client;

        //Action to send info about the number of participants to the UI below
        private readonly Action<int> _callbackParticipantCount;

        public UIAddFriends(User user, Action<int> callbackParticipantCount)
        {
            InitializeComponent();
            _client = new RealLifeServiceClient();

            ListFriends(user);
            _callbackParticipantCount = callbackParticipantCount;
        }

        private void ListFriends(User user)
        {
            lbxAddFriends.ItemsSource = _client.GetFriendships(user);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                List<User> participantList = lbxAddFriends.SelectedItems.Cast<User>().ToList();
                PageCreateAchievement.participantList = participantList;

                if (participantList.Any())
                {
                    //This action sends back the count of participants to the calling UI
                    _callbackParticipantCount(participantList.Count);
                }

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}